﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Animation;

namespace NJT.动画
{
    public class 动画
    {
        public void 启动动画(FrameworkElement 画布, FrameworkElement 对象, DependencyProperty 对象属性, double 起点, double 终点,
            TimeSpan 时长, int 循环次数)
        {
            if (循环次数 == 0)
                return;
            对象.Name = "n" + DateTime.Now.Ticks.ToString();
            NameScope.SetNameScope(画布, new NameScope());

            画布.RegisterName(对象.Name, 对象);
            var myDoubleAnimation = new DoubleAnimation();

            myDoubleAnimation.From = 起点;
            myDoubleAnimation.To = 终点;
            myDoubleAnimation.Duration = new Duration(时长);
            Storyboard.SetTargetName(myDoubleAnimation, 对象.Name);
            Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(对象属性));

            var myStoryboard = new Storyboard();
            myStoryboard.Children.Add(myDoubleAnimation);
            myStoryboard.RepeatBehavior = 循环次数 < 0 ? RepeatBehavior.Forever : new RepeatBehavior(循环次数);

            myStoryboard.Begin(对象);
        }
    }
}
